<?php
session_start();

require_once 'meekrodb.2.2.class.php';
require_once 'app_config.php';
//require_once 'MySQL.dll.php';
require_once 'common_function.php';
require_once 'paging.cls.php';
require_once 'resizejpg.php';
require_once 'value.php';

//require all class
require_once '../classes/app.php'; 
foreach ($app_pages as $key => $value) {
	require_once '../classes/'.$value.'.php';
}

function find($sql = null, $type = 'all') {
	var_dump($sql);
    $db = new MySQL();
    $sql = $db->select($sql);
    $return = null;

    switch ($type) {
        case "single":
            if ($db->numrows($sql) > 0) {
                $return = $db->fetch($sql);
            }
            break;
        case "count":
            $return = $db->numrows($sql);
            break;
        default:
            $return = array();
            if ($db->numrows($sql) > 0) {
                while ($row = $db->fetch($sql)) {
                    array_push($return, $row);
                }
            }

            break;
    }

    $db->close();

    return $return;
}
	
function update($sql){
	$db = new MySQL();
    $sql = $db->select($sql);		
	$return = $db->update($query);								
	$db->close();				
	return $return;
}

function createAction($mod = null, $id = null) {
    return "?mod=$mod&act=process&id=$id";
}

function uploadFile($name, $folder, $size_x = 0, $size_y = 0, &$image) { //$image: remove old file(for update)
    if ($_FILES[$name]['name'] != "") {

        $parts1 = pathinfo($_FILES[$name]['name']);
        $ext1 = "." . strtolower($parts1["extension"]);


        if (file_exists("../$folder/" . $_FILES[$name]["name"])) {
            $attach_file = date("sihdmy") . $ext1;
        } else {
            $attach_file = str_replace(" ", "_", $_FILES[$name]["name"]);
        }

        if (move_uploaded_file($_FILES[$name]["tmp_name"], "../$folder/" . $attach_file)) {
            $filemod = "../$folder/" . $attach_file;

            chmod($filemod, 0777);
            
            if(!empty($size_x) || !empty($size_y)){
                resize_jpg($filemod, $filemod, 480, 0);
            }


            if (!empty($image) && file_exists("../$folder/" . $image)) {
                unlink("../$folder/" . $image);
            }
            $image = $attach_file;
        }
    }
}

function createField($config=null) {
    /*
    $config = array(
        'name' => "image1",
        'type' => 'file',
        'folder' => 'home-img',
        'title' => 'Image 1',
        'editor' => true,
        'size_x' => 310,
        'size_y' => 180,
        'value' => ""
    );*/
    // var_dump($config);
    
    switch ($config['type']) {
        case "file":
            $image = "";
            if(!empty($config['value'])){
                $image = '<br/><img class="img-thumbnail" src="../' .$config['folder']. '/' .$config['value']. '"  />';
            }
            
            $field = "<div class=\"form-group\">
                            <label class=\"col-sm-3 control-label\">" .$config['title']. " (".$config['size_x']."px*".$config['size_y']."px) : </label>
                            <div class=\"col-sm-9\">
                                <input type=\"file\" name=\"".$config['name']."\"  />
                                $image
                            </div>
                        </div>";
             
            break;
        
        case "text":
            $field = '<div class="form-group">
                        <label class="col-sm-3 control-label">'.$config['title'].' :</label>
                        <div class="col-sm-9">
                            <input name="'.$config['name'].'" type="text" class="form-control" value="'. $config['value'] .'" >
                        </div>
                    </div>';

            break;

        default:
            break;
    }
    
    return $field;
}

function removeBadCharacters($s)
{
   return str_replace(array('&','<','>','/','\\','"',"'",'?','+'), '', $s);
}